<template>
  <div class="login">
    <el-form
      :model="userData"
      status-icon
      :rules="rules"
      ref="userData"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="userData.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="userData.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('userData')"
          :disabled="disable"
          :loading="disable"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import loginmixin from "@/Mixin/loginMixin";
export default {
  data() {
    return {
      disable: false,
      userData: {
        password: "",
        username: "",
      },
      rules: {
        password: [
          { type: "string", required: true, message: "请输入用户密码" },
          { validator: this.validatePass },
        ],
        username: [
          { type: "string", required: true, message: "请输入用户昵称" },
          { validator: this.validateName },
        ],
      },
    };
  },
  mixins: [loginmixin],
  methods: {
    submitForm(formName) {
      this.disable = true
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let ret = await this.$store.dispatch('login/dologin',this.userData)
          if(ret){
            this.$router.replace('/home')
            location.href = '/home'
          }else{
            this.$message({
            message: "用户名和密码要填写正确",
            type: "error",
            duration: 1500,
            onClose: () => {
              this.disable = false;
            },
          })
          }
        } else {
          this.$message({
            message: "用户名和密码要填写正确",
            type: "error",
            duration: 1500,
            onClose: () => {
              this.disable = false;
            },
          });
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 500px;
  margin: 150px auto;
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 50px 10px;
}
</style>